home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / usr_02.txt < prev    next >
Encoding:
Text File  |  2001-09-26  |  16.3 KB  |  495 lines

  1. *usr_02.txt*    For Vim version 6.0.  Last change: 2001 Sep 03
  2.  
  3.              VIM USER MANUAL - by Bram Moolenaar
  4.  
  5.                 The first steps in Vim
  6.  
  7.  
  8. This chapter provides just enough information to edit a file with Vim.  Not
  9. well or fast, but you can edit.  Take some time to practice with these
  10. commands, they form the base for what follows.
  11.  
  12. |02.1|    Running Vim for the First Time
  13. |02.2|    Inserting text
  14. |02.3|    Moving around
  15. |02.4|    Deleting characters
  16. |02.5|    Undo and Redo
  17. |02.6|    Other editing commands
  18. |02.7|    Getting out
  19. |02.8|    Finding help
  20.  
  21.      Next chapter: |usr_03.txt|  Moving around
  22.  Previous chapter: |usr_01.txt|  About the manuals
  23. Table of contents: |usr_toc.txt|
  24.  
  25. ==============================================================================
  26. *02.1*    Running Vim for the First Time
  27.  
  28. To start Vim, enter this command: >
  29.  
  30.     gvim file.txt
  31.  
  32. In UNIX you can type this at any command prompt.  If you are running Microsoft
  33. Windows, open an MS-DOS prompt window and enter the command.
  34.    In either case, Vim starts editing a file called file.txt.  Because this
  35. is a new file, you get a blank window. This is what your screen will look
  36. like:
  37.  
  38.     +---------------------------------------+
  39.     |#                    |
  40.     |~                    |
  41.     |~                    |
  42.     |~                    |
  43.     |~                    |
  44.     |"file.txt" [New file]            |
  45.     +---------------------------------------+
  46.         ('#" is the cursor position.)
  47.  
  48. The tilde (~) lines indicate lines not in the file.  In other words, when Vim
  49. runs out of file to display, it displays tilde lines.  At the bottom of the
  50. screen, a message line indicates the file is named file.txt and shows that you
  51. are creating a new file.  The message information is temporary and other
  52. information overwrites it.
  53.  
  54.  
  55. THE VIM COMMAND
  56.  
  57. The gvim command causes the editor to create a new window for editing.  If you
  58. use this command: >
  59.  
  60.     vim file.txt
  61.  
  62. the editing occurs inside your command window.  In other words, if you are
  63. running inside an xterm, the editor uses your xterm window.  If you are using
  64. an MS-DOS command prompt window under Microsoft Windows, the editing occurs
  65. inside this window.  The text in the window will look the same for both
  66. versions, but with gvim you have extra features, like a menu bar.  More about
  67. that later.
  68.  
  69. ==============================================================================
  70. *02.2*    Inserting text
  71.  
  72. The Vim editor is a modal editor.  That means that the editor behaves
  73. differently, depending on which mode you are in.  The two basic modes are
  74. called Normal mode and Insert mode.  In Normal mode the characters you type
  75. are commands.  In Insert mode the characters are inserted as text.
  76.    Since you have just started Vim it will be in Normal mode.  To start Insert
  77. mode you type the "i" command (i for Insert).  Then you can enter
  78. the text.  It will be inserted into the file.  Do not worry if you make
  79. mistakes; you can correct them later.  To enter the following programmer's
  80. limerick, this is what you type: >
  81.  
  82.     iA very intelligent turtle
  83.     Found programming UNIX a hurdle
  84.  
  85. After typing "turtle" you press the <Enter> key to start a new line.  Finally
  86. you press the <Esc> key to stop Insert mode and go back to Normal mode.  You
  87. now have two lines of text in your Vim window:
  88.  
  89.     +---------------------------------------+
  90.     |A very intelligent turtle        |
  91.     |Found programming UNIX a hurdle    |
  92.     |~                    |
  93.     |~                    |
  94.     |                    |
  95.     +---------------------------------------+
  96.  
  97.  
  98. WHAT IS THE MODE?
  99.  
  100. To be able to see what mode you are in, type this command: >
  101.  
  102.     :set showmode
  103.  
  104. You will notice that when typing the colon Vim moves the cursor to the last
  105. line of the window.  That's where you type colon commands (commands that start
  106. with a colon).  Finish this command by pressing the <Enter> key (all commands
  107. that start with a colon are finished this way).
  108.    Now, if you type the "i" command Vim will display --INSERT-- at the bottom
  109. of the window.  This indicates you are in Insert mode.
  110.  
  111.     +---------------------------------------+
  112.     |A very intelligent turtle        |
  113.     |Found programming UNIX a hurdle    |
  114.     |~                    |
  115.     |~                    |
  116.     |-- INSERT --                |
  117.     +---------------------------------------+
  118.  
  119. If you press <Esc> to go back to Normal mode the last line will be made blank.
  120.  
  121.  
  122. GETTING OUT OF TROUBLE
  123.  
  124. One of the problems for Vim novices is mode confusion, which is caused by
  125. forgetting which mode you are in or by accidentally typing a command that
  126. switches modes.  To get back to Normal mode, no matter what mode you are in,
  127. press the <Esc> key.  Sometimes you have to press it twice.  If Vim beeps back
  128. at you, you already are in Normal mode.
  129.  
  130. ==============================================================================
  131. *02.3*    Moving around
  132.  
  133. After you return to command mode, you can move around by using these keys:
  134.  
  135.     h   left
  136.     j   down
  137.     k   up
  138.     l   right
  139.  
  140. At first, it may appear that these commands were chosen at random.  After all,
  141. who ever heard of using l for right?  But actually, there is a very good
  142. reason for these choices: Moving the cursor is the most common thing you do in
  143. an editor, and these keys are on the home row of your right hand.  In other
  144. words, these commands are placed where you can type them the fastest
  145. (especially when you type with ten fingers).
  146.  
  147.     Note:
  148.     You can also move the cursor by using the arrow keys.  If you do,
  149.     however, you greatly slow down your editing because to press the arrow
  150.     keys, you must move your hand from the text keys to the arrow keys.
  151.     Considering that you might be doing it hundreds of times an hour, this
  152.     can take a significant amount of time.
  153.        Also, there are keyboards which do not have arrow keys, or which
  154.     locate them in unusual places; therefore, knowing the use of the hjkl
  155.     keys helps in those situations.
  156.  
  157. One way to remember these commands is that h is on the left, l is on the
  158. right and j points down.  In a picture: >
  159.  
  160.                k
  161.            h     l
  162.              j
  163.  
  164. The best way to learn these commands is by using them.  Use the "i" command to
  165. insert some more lines of text.  Then use the hjkl keys to move around and
  166. insert a word somewhere.  Don't forget to press <Esc> to go back to Normal
  167. mode.  The |vimtutor| is also a nice way to learn by doing.
  168.  
  169. ==============================================================================
  170. *02.4*    Deleting characters
  171.  
  172. To delete a character, move the cursor over it and type "x".  (This is a
  173. throwback to the old days of the typewriter, when you deleted things by typing
  174. xxxx over them.)  Move the cursor to the beginning of the first line, for
  175. example, and type xxxxxxx (seven x's) to delete "A very ".  The result should
  176. look like this:
  177.  
  178.     +---------------------------------------+
  179.     |intelligent turtle            |
  180.     |Found programming UNIX a hurdle    |
  181.     |~                    |
  182.     |~                    |
  183.     |                    |
  184.     +---------------------------------------+
  185.  
  186. Now you can insert new text, for example by typing: >
  187.  
  188.     iA young <Esc>
  189.  
  190. This begins an insert (the i), inserts the words "A young", and then exits
  191. insert mode (the final <Esc>).    The result:
  192.  
  193.     +---------------------------------------+
  194.     |A young intelligent turtle        |
  195.     |Found programming UNIX a hurdle    |
  196.     |~                    |
  197.     |~                    |
  198.     |                    |
  199.     +---------------------------------------+
  200.  
  201.  
  202. DELETING A LINE
  203.  
  204. To delete a whole line use the "dd" command.  The following line will
  205. then move up to fill the gap:
  206.  
  207.     +---------------------------------------+
  208.     |Found programming UNIX a hurdle    |
  209.     |~                    |
  210.     |~                    |
  211.     |~                    |
  212.     |                    |
  213.     +---------------------------------------+
  214.  
  215.  
  216. DELETING A LINE BREAK
  217.  
  218. In Vim you can join two lines together, which means that the line break
  219. between them is deleted.  The "J" command does this.
  220.    Take these two lines:
  221.  
  222.     A young intelligent ~
  223.     turtle ~
  224.  
  225. Move the cursor to the first line and press "J":
  226.  
  227.     A young intelligent turtle ~
  228.  
  229. ==============================================================================
  230. *02.5*    Undo and Redo
  231.  
  232. Suppose you delete too much.  Well, you can type it in again, but an easier
  233. way exists.  The "u" command undoes the last edit.  Take a look at this in
  234. action: After using "dd" to delete the first line, "u" brings it back.
  235.    Another one: Move the cursor to the A in the first line:
  236.  
  237.     A young intelligent turtle ~
  238.  
  239. Now type xxxxxxx to delete "A young".  The result is as follows:
  240.  
  241.      intelligent turtle ~
  242.  
  243. Type "u" to undo the last delete.  That delete removed the g, so the undo
  244. restores the character.
  245.  
  246.     g intelligent turtle ~
  247.  
  248. The next u command restores the next-to-last character deleted:
  249.  
  250.     ng intelligent turtle ~
  251.  
  252. The next u command gives you the u, and so on:
  253.  
  254.     ung intelligent turtle ~
  255.     oung intelligent turtle ~
  256.     young intelligent turtle ~
  257.      young intelligent turtle ~
  258.     A young intelligent turtle ~
  259.  
  260.     Note:
  261.     If you type "u" twice, and the result is that you get the same text
  262.     back, you have Vim configured to work Vi compatible.  Look here to fix
  263.     this: |not-compatible|.
  264.        This text assumes you work "The Vim Way".  You might prefer to use
  265.     the good old Vi way, but you will have to watch out for small
  266.     differences in the text then.
  267.  
  268.  
  269. REDO
  270.  
  271. If you undo too many times, you can press CTRL-R (redo) to reverse the
  272. preceding command.  In other words, it undoes the undo.  To see this in
  273. action, press CTRL-R twice.  The character A and the space after it disappear:
  274.  
  275.     young intelligent turtle ~
  276.  
  277. There's a special version of the undo command, the "U" (undo line) command.
  278. The undo line command undoes all the changes made on the last line that was
  279. edited.  Typing this command twice cancels the preceding "U".
  280.  
  281.     A very intelligent turtle ~
  282.       xxxx                Delete very
  283.  
  284.     A intelligent turtle ~
  285.               xxxxxx        Delete turtle
  286.  
  287.     A intelligent ~
  288.                     Restore line with "U"
  289.     A very intelligent turtle ~
  290.                     Undo "U" with "u"
  291.     A intelligent ~
  292.  
  293. The "U" command is a change by itself, which the "u" command undoes and CTRL-R
  294. redoes.  This might be a bit confusing.  Don't worry, with "u" and CTRL-R you
  295. can go to any of the situations you had.
  296.  
  297. ==============================================================================
  298. *02.6*    Other editing commands
  299.  
  300. Vim has a large number of commands to change the text.  See |Q_in| and below.
  301. Here are a few often used ones.
  302.  
  303.  
  304. APPENDING
  305.  
  306. The "i" command inserts a character before the character under the cursor.
  307. That works fine; but what happens if you want to add stuff to the end of the
  308. line?  For that you need to insert text after the cursor.  This is done with
  309. the "a" (append) command.
  310.    For example, to change the line
  311.  
  312.     and that's not saying much for the turtle. ~
  313. to
  314.     and that's not saying much for the turtle!!! ~
  315.  
  316. move the cursor over to the dot at the end of the line. Then type "x" to
  317. delete the period.  The cursor is now positioned at the end of the line on the
  318. e in turtle.  Now type >
  319.  
  320.     a!!!<Esc>
  321.  
  322. to append three exclamation points after the e in turtle:
  323.  
  324.     and that's not saying much for the turtle!!! ~
  325.  
  326.  
  327. OPENING UP A NEW LINE
  328.  
  329. The "o" command creates a new, empty line below the cursor and puts Vim in
  330. Insert mode.  Then you can type the text for the new line.
  331.    Suppose the cursor is somewhere in the first of these two lines:
  332.  
  333.     A very intelligent turtle ~
  334.     Found programming UNIX a hurdle ~
  335.  
  336. If you now use the "o" command and type new text: >
  337.  
  338.     oThat liked using Vim<Esc>
  339.  
  340. The result is:
  341.  
  342.     A very intelligent turtle ~
  343.     That liked using Vim ~
  344.     Found programming UNIX a hurdle ~
  345.  
  346. The "O" command (uppercase) opens a line above the cursor.
  347.  
  348.  
  349. USING A COUNT
  350.  
  351. Suppose you want to move up nine lines.  You can type "kkkkkkkkk" or you can
  352. enter the command "9k".  In fact, you can precede many commands with a number.
  353. Earlier in this chapter, for instance, you added three exclamation points to
  354. the end of a line by typing "a!!!<Esc>".  Another way to do this is to use the
  355. command "3a!<Esc>".  The count of 3 tells the command that follows to triple
  356. its effect.  Similarly, to delete three characters, use the command "3x".  The
  357. count always comes before the command it applies to.
  358.  
  359. ==============================================================================
  360. *02.7*    Getting out
  361.  
  362. To exit, use the "ZZ" command.  This command writes the file and exits.
  363.  
  364.     Note:
  365.     Unlike many other editors, Vim does not automatically make a backup
  366.     file.  If you type "ZZ", your changes are committed and there's no
  367.     turning back.  You can configure the Vim editor to produce backup
  368.     files, see |07.4|.
  369.  
  370.  
  371. DISCARDING CHANGES
  372.  
  373. Sometimes you will make a sequence of changes and suddenly realize you were
  374. better off before you started.  Not to worry; Vim has a
  375. quit-and-throw-things-away command.  It is: >
  376.  
  377.     :q!
  378.  
  379. Don't forget to press <Enter> to finish the command.
  380.  
  381. For those of you interested in the details, the three parts of this command
  382. are the colon (:), which enters command mode; the q command, which tells the
  383. editor to quit; and the override command modifier (!).
  384.    The override command modifier is needed because Vim is reluctant to throw
  385. away changes.  If you were to just type ":q", Vim would display an error
  386. message and refuse to exit:
  387.  
  388.     E37: No write since last change (use ! to override) ~
  389.  
  390. By specifying the override, you are in effect telling Vim, "I know that what
  391. I'm doing looks stupid, but I'm a big boy and really want to do this."
  392.  
  393. If you want to continue editing with Vim: The ":e!" command reloads the
  394. original version of the file.
  395.  
  396. ==============================================================================
  397. *02.8*    Finding help
  398.  
  399. Everything you always wanted to know can be found in the Vim help files.
  400. Don't be afraid to ask!
  401.    To get generic help use this command: >
  402.  
  403.     :help
  404.  
  405. You could also use the first function key <F1>.  If your keyboard has a <Help>
  406. key it might work as well.
  407.    If you don't supply a subject, ":help" displays the general help window.
  408. The creators of Vim did something very clever (or very lazy) with the help
  409. system: They made the help window a normal editing window.  You can use all
  410. the normal Vim commands to move through the help information.  Therefore h, j,
  411. k, and l move left, down, up and right.
  412.    To get out of the help window, use the same command you use to get out of
  413. the editor: "ZZ".  This will only close the help window, not exit Vim.
  414.  
  415. As you read the help text, you will notice some text enclosed in vertical bars
  416. (for example, |help|).  This indicates a hyperlink.  If you position the
  417. cursor anywhere between the bars and press CTRL-] (jump to tag), the help
  418. system takes you to the indicated subject. (For reasons not discussed here,
  419. the Vim terminology for a hyperlink is tag.  So CTRL-] jumps to the location
  420. of the tag given by the word under the cursor.)
  421.    After a few jumps, you might want to go back.  CTRL-T (pop tag) takes you
  422. back to the preceding position.  CTRL-O (jump to older position) also works
  423. nicely here.
  424.    At the top of the help screen, there is the notation *help.txt*.  This name
  425. between "*" characters is used by the help system to define a tag (hyperlink
  426. destination).
  427.    See |29.1| for details about using tags.
  428.  
  429. To get help on a given subject, use the following command: >
  430.  
  431.     :help {subject}
  432.  
  433. To get help on the "x" command, for example, enter the following: >
  434.  
  435.     :help x
  436.  
  437. To find out how to delete text, use this command: >
  438.  
  439.     :help deleting
  440.  
  441. To get a complete index of all Vim commands, use the following command: >
  442.  
  443.     :help index
  444.  
  445. When you need to get help for a control character command (for example,
  446. CTRL-A), you need to spell it with the prefix "CTRL-". >
  447.  
  448.     :help CTRL-A
  449.  
  450. The Vim editor has many different modes.  By default, the help system displays
  451. the normal-mode commands.  For example, the following command displays help
  452. for the normal-mode CTRL-H command: >
  453.  
  454.     :help CTRL-H
  455.  
  456. To identify other modes, use a mode prefix.  If you want the help for the
  457. insert-mode version of a command, use "i_".  For CTRL-H this gives you the
  458. following command: >
  459.  
  460.     :help i_CTRL-H
  461.  
  462. When you start the Vim editor, you can use several command-line arguments.
  463. These all begin with a dash (-).  To find what the -t argument does, for
  464. example, use the command: >
  465.  
  466.     :help -t
  467.  
  468. The Vim editor has a number of options that enable you to configure and
  469. customize the editor.  If you want help for an option, you need to enclose it
  470. in single quotation marks.  To find out what the 'number' option does, for
  471. example, use the following command: >
  472.  
  473.     :help 'number'
  474.  
  475. The table with all mode prefixes can be found here: |help-context|.
  476.  
  477. Special keys are enclosed in angle brackets. To find help on the up-arrow key
  478. in Insert mode, for instance, use this command: >
  479.  
  480.     :help i_<Up>
  481.  
  482. If you see an error message that you don't understand, for example:
  483.  
  484.     E37: No write since last change (use ! to override) ~
  485.  
  486. You can use the error ID at the start to find help about it: >
  487.  
  488.     :help E37
  489.  
  490. ==============================================================================
  491.  
  492. Next chapter: |usr_03.txt|  Moving around
  493.  
  494. Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl:
  495.